﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Server
{
    public partial class Server : Form
    {
        public Server()
        {
            InitializeComponent();
            hp = new ServerHelper();
        }
        TcpListener nghea,server;
        int index = 0;
        TcpClient[] client = new TcpClient[256];
        TcpClient client_tmp;
        string ip = "", LastIp;
        string ipClient = "";
        Thread langnghe, thdHandler;
        bool flag = false;
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter gui = new StreamWriter(client[index].GetStream());
            gui.WriteLine(ipClient + "$0$TatMay");
            gui.Flush();

        }
        public void LangNghe()
        {
            nghea = new TcpListener(8080);
            nghea.Start();
            while(true)
            {
                try
                {
                    client_tmp = nghea.AcceptTcpClient();
                    NetworkStream stream = new NetworkStream(client_tmp.Client);
                    byte[] data = new byte[1024];
                    int receiv = client_tmp.Client.Receive(data);
                    ipClient = Encoding.UTF8.GetString(data, 0, receiv).TrimEnd();
                    string[] nhap = ipClient.Split('.');
                    index = Int32.Parse(nhap[3]);
                    client[index] = client_tmp;
                    lbxDanhSach.Items.Add(ipClient);
                    lbxDanhSach.SelectedItem = ipClient;
                    txtIP.Text = ipClient;
                }
                catch (Exception)
                {
                    nghea.Stop();
                }
                thdHandler = new Thread(new ThreadStart(NhanDuLieu));
                thdHandler.Start();
            }
            
            
        }
        public void NhanDuLieu()
        {
            NetworkStream stream = new NetworkStream(client[index].Client);
            byte[] data = new byte[1024];
            string ndung="";
            while (true)
            {
                try
                {
                    int receiv = client[index].Client.Receive(data);
                    ndung = Encoding.UTF8.GetString(data, 0, receiv).TrimEnd();
                    lbxChat.Items.Add(ndung);
                }
                catch (Exception err1)
                {
                    MessageBox.Show(err1.Message.ToString(), "Loi!!!");
                    if (client != null) 
                        client[index].Client.Close();
                }
            }
        }
        private void Server_Load(object sender, EventArgs e)
        {
            langnghe = new Thread(new ThreadStart(LangNghe));
            langnghe.Start();
            ip = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
        }

        private void lbxDanhSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtIP.Text = lbxDanhSach.SelectedItem.ToString();
            ipClient = txtIP.Text;
            string[] rose = ipClient.Split('.');
            index = Int32.Parse(rose[3]);
        }

        private void logoffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter gui = new StreamWriter(client[index].GetStream());
            gui.WriteLine(ipClient + "$0$LogOff");
            gui.Flush();
        }

        private void btnGui_Click(object sender, EventArgs e)
        {
            StreamWriter gui = new StreamWriter(client[index].GetStream());
            ipClient = lbxDanhSach.SelectedItem.ToString();
            gui.WriteLine(ipClient + "$1$Server: " + txtChat.Text);
            gui.Flush();
            lbxChat.Items.Add("Tôi: " + txtChat.Text);
            txtChat.Text = "";
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter gui = new StreamWriter(client[index].GetStream());
            gui.WriteLine(ipClient + "$0$ReStart");
            gui.Flush();
        }

        private void powerToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void gửiFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsMdiContainer = true;
            SendFile form = new SendFile(txtIP.Text);
            form.Show();

        }
        ServerHelper hp = new ServerHelper();
        private void start_Click(object sender, EventArgs e)
        {
            hp.StartServer();
        }
     
        
    }
}
